package com.hpf.lease.web.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hpf.lease.common.config.CommonException;
import com.hpf.lease.common.result.ResultCodeEnum;
import com.hpf.lease.model.bean.AttrKey;
import com.hpf.lease.web.admin.vo.AttrInfoVo;
import com.hpf.lease.web.admin.mapper.AttrKeyMapper;
import com.hpf.lease.web.admin.service.AttrKeyService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author GOD
* @description 针对表【attr_key(房间基本属性表)】的数据库操作Service实现
* @createDate 2024-08-29 15:00:50
*/
@Service
public class AttrKeyServiceImpl extends ServiceImpl<AttrKeyMapper, AttrKey>
    implements AttrKeyService {
    @Resource
    private AttrKeyMapper attrKeyMapper;

    @Override
    public boolean saveOrUpdateInfo(AttrKey attrKey) {
        Long id = attrKey.getId() == null ? -1L : attrKey.getId();
        AttrKey one = this.getOne(Wrappers.lambdaQuery(AttrKey.class)
                .eq(AttrKey::getId, attrKey.getId())
                .eq(AttrKey::getName, attrKey.getName())
                .last("limit 1")
        );
        if (one != null && !attrKey.getId().equals(id)){
            throw new CommonException(ResultCodeEnum.ATTR_KEY_EXISTS_ERROR);
        }
        return this.saveOrUpdate(attrKey);
    }

    @Override
    public List<AttrInfoVo> selectAll() {
        return attrKeyMapper.selectAll();
    }
}




